'''爬取排行榜'''
import pandas as pd
import requests
from bs4 import BeautifulSoup
import csv
import datetime
from lxml import etree

url = 'https://www.bilibili.com/v/popular/rank/all'
headers = {
   'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62"
}
#发起网络请求
response = requests.get(url=url, headers=headers)
response.encoding = 'utf-8'
page_text = response.text
tree = etree.HTML(page_text)
li_list = tree.xpath("//div[@class='rank-list-wrap']/ul/li")
videos = []
class video:
   def __init__(self, rank, title, score, play_num, danmu_num, up_name, up_space, play_address):
       self.rank = rank
       self.title = title
       self.score = score
       self.play_num = play_num
       self.danmu_num = danmu_num
       self.up_name = up_name
       self.up_space = up_space
       self.play_address = play_address
   def to_csv(self):
      return [self.rank, self.title, self.score, self.play_num, self.danmu_num, self.up_name, self.up_space, self.play_address]
   @staticmethod
   def csv_info():
      return ['排行', '标题', '综合得分', '播放量', '弹幕量', 'UP名', 'UP空间地址', '视频链接']

for li in li_list:
   rank = li.xpath("./@data-rank")[0]
   title = li.xpath('.//div[@class="info"]/a[@class="title"]')[0].text
   play_address ='https:' + li.xpath(".//div[@class='info']/a/@href")[0]
   #img = li.xpath('.//div[@class="img"]/a/img/@src')
   play_num = li.xpath('.//div[@class="detail"]/span[1]/text()')[0]
   danmu_num = li.xpath('.//div[@class="detail"]/span[2]/text()')[0]
   up_name = li.xpath('.//div[@class="detail"]/a/span/text()')[0]
   score = li.xpath('.//div[@class="pts"]/div/text()')[0]
   up_space ='https:' + li.xpath('.//div[@class="detail"]/a/@href')[0]
   v = video(rank, title, score, play_num, danmu_num, up_name, up_space, play_address)
   videos.append(v)

now_str = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
file_name = f'top100_{now_str}.csv'
with open(file_name, 'w', newline='', encoding='utf-8-sig') as f:
   pen = csv.writer(f)
   pen.writerow(video.csv_info())
   for v in videos:
       pen.writerow(v.to_csv())